<template>
  <div class="login-container">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item prop="loginName">
        <el-input
          v-model="form.loginName"
          placeholder="admin"
          type="text"
          :autofocus="true"
        >
          <template #prefix>
            <span class="el-icon-phone" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="password"
          :autofocus="true"
        >
          <template #prefix>
            <svg-icon icon-class="password" />
          </template>
          <template #suffix>
            <svg-icon icon-class="eye" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          v-model="form.code"
          placeholder="请输入验证码"
          :autofocus="true"
        >
          <template #prefix>
            <span class="el-icon-folder-checked" />
          </template>
          <template #suffix>
            <div class="codeImg" @click="getCode">
              <img :src="codeImgSrc" alt="">
            </div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="login-btn" type="primary" style="width:100%;background-color:#6877e4;height:50px" :loading="loading" @click="onSubmit">登录</el-button>
      </el-form-item>
      <img src="https://likede2-admin.itheima.net/img/logo.595745bd.png" alt="" class="login-logo">
    </el-form>
  </div>
</template>
<script>
import { codeImgApi } from '@/api/user.js'
// loginApi,
export default {
  data() {
    return {
      form: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: '',
        loginType: 0

      },
      rules: {
        loginName: [{ required: true, message: '请填写账号', trigger: 'blur' }],
        password: [{ required: true, message: '请填写密码', trigger: 'blur' }],
        code: [{ required: true, message: '请填写验证码', trigger: 'blur' }]
      },
      codeImgSrc: 'https://likede2-java.itheima.net/api/user-service/user/imageCode/kEFp8kMSq7Tg0FrQo82nJTCSJrNK62l3',
      loading: false
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    async  getCode() {
      const randomNumber = Math.round(Math.random() * 1000)
      this.form.clientToken = randomNumber
      // console.log(randomNumber)
      const data = await codeImgApi(randomNumber)
      // console.log(data, '验证码')
      const bufferUrl = btoa(
        new Uint8Array(data).reduce(
          (data, byte) => data + String.fromCharCode(byte), '')
      )
      const imgUrl = 'data:image/png;base64,' + bufferUrl
      // console.log(imgUrl)
      this.codeImgSrc = imgUrl
    },
    async  onSubmit() {
      try {
        // const res = await loginApi(this.form)
      // console.log(this.form)
        this.loading = true
        await this.$refs.form.validate()
        await this.$store.dispatch('user/loginAction', this.form)

        // console.log('跳转前')
        // this.$router.push('/')
        // console.log('跳转后')

        this.loading = false
      } catch (error) {
        console.log(error, 'error')
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
.login-container{
   background-image: url('https://likede2-admin.itheima.net/img/background.be4fae7d.png');
   position: relative;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background-repeat: no-repeat;
    background-size: cover;
    box-sizing: border-box;
}
::v-deep .el-form{
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: white;
    box-shadow: 0 3px 70px 0 rgba(30,111,72,.35);
    border-radius: 10px;
}
.login-logo{
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
}
  ::v-deep .el-form-item__content{
      background-color: white;

    }
    ::v-deep .el-input {
      color: black !important;
      width:446px;
      height: 50px;

    }

  ::v-deep input.el-input__inner{
    margin-left: 20px;
    width:100%;
    color:black;
    caret-color:grey
  }
  .codeImg{
    width: 130px;
    height: 50px;
    display: block;
    background-color: pink;
  }
</style>
